package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.GetUserOrgsReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.RpcUserInfoVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserOrgInfoResVO;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.CandidateRelationShipEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserOrgService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserRelationShipService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.FlowApplyUserComponentUtil;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.User;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.OrgInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserRelationShip;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowUserRelationShipRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.UserRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
　　* @description: 用户关系服务实现
　　* @author shenrm
　　* @date 2021/11/3 15:02
　　*/
@Service
public class UserRelationShipServiceImpl implements UserRelationShipService {

    @Autowired
    private FlowUserRelationShipRepository flowUserRelationShipRepository;

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private FlowApplyUserComponentUtil flowApplyUserComponentUtil;

    @Autowired
    private UserOrgService userOrgService;

    @Override
    public List<UserRelationShip> getUserRelationShipList() {
        return flowUserRelationShipRepository.getUserRelationShipList();
    }

    @Override
    public Set<String> getUserByUserRelatOrgId(String userId) {

        List<OrgInfo> orgInfoList = userOrgService.getOrgInfoByUserId(userId);

        if (CollectionUtils.isEmpty(orgInfoList)){
            return null;
        }

        List<String> orgCodeList = orgInfoList.stream().map(OrgInfo::getOrgCode).collect(Collectors.toList());

        Set<String> userSet = new HashSet<>();
        List<UserInfo> userInfoList = userOrgService.getUserListByOrg(orgCodeList);
        if (CollectionUtils.isNotEmpty(userInfoList)){
            for (UserInfo userInfo : userInfoList) {
                userSet.add(userInfo.getUserCode());
            }
            return userSet;
        }

        return null;
    }

    @Override
    public Map<String, List> getRelationShipList(String processInstanceId, String type, String applyUser, String userCode) {
        Map<String, List> rtmap = new HashMap();
        String userId = "";
        // 拟稿人或拟稿人部门,则获取拟稿人ID
        if(CandidateRelationShipEnum.NGR.getCode().equals(type) ||
                CandidateRelationShipEnum.NGRBM.getCode().equals(type)) {
            // 根据流程实例ID获取拟稿人ID
            userId = flowApplyUserComponentUtil.getApplyUser(processInstanceId, applyUser);
        } else if(CandidateRelationShipEnum.DQRBM.getCode().equals(type)){
            userId = userCode;
        }

        // 拟稿人部门或当前人部门
        if(StringUtils.isNotBlank(userId)) {
            if(CandidateRelationShipEnum.NGR.getCode().equals(type)) {
                SelectUserInfoReqVO reqVO = new SelectUserInfoReqVO();
                reqVO.setUserId(Long.parseLong(userId));
                BaseResult<RpcUserInfoVO> rpcUserInfoVos = userFeignService.qryUserInfo(reqVO);
                if(Objects.isNull(rpcUserInfoVos)) {
                    return null;
                }
                RpcUserInfoVO rpcUserInfoVO = rpcUserInfoVos.getData();
                if(Objects.isNull(rpcUserInfoVO)) {
                    return null;
                }

                // 拟稿人集合
                List<UserInfo> userInfos = new ArrayList<>();
                UserInfo userInfo = new UserInfo();
                userInfo.setUserCode(String.valueOf(rpcUserInfoVO.getUserId()));
                userInfo.setUserName(rpcUserInfoVO.getUserName());
                userInfos.add(userInfo);
                rtmap.put("data", userInfos);
            } else if(CandidateRelationShipEnum.NGRBM.getCode().equals(type) ||
                    CandidateRelationShipEnum.DQRBM.getCode().equals(type)){
                GetUserOrgsReqVO reqVO = new GetUserOrgsReqVO();
                reqVO.setUserId(Long.parseLong(userId));
                BaseResult<List<UserOrgInfoResVO>> userOrgInfoResVos = userFeignService.getOrgsByUser(reqVO);
                if(Objects.isNull(userOrgInfoResVos)) {
                    return null;
                }
                List<UserOrgInfoResVO> userOrgInfoResVoList = userOrgInfoResVos.getData();
                if(CollectionUtils.isEmpty(userOrgInfoResVoList)) {
                    return null;
                }

                // 拟稿人部门组织集合
                List<OrgInfo> orgInfos = new ArrayList<>();
                OrgInfo orgInfo;
                for (UserOrgInfoResVO vo : userOrgInfoResVoList){
                    orgInfo = new OrgInfo();
                    orgInfo.setOrgCode(String.valueOf(vo.getOrgId()));
                    orgInfo.setOrgName(vo.getOrgName());
                    orgInfos.add(orgInfo);
                }
                rtmap.put("data", orgInfos);
            }
            return rtmap;
        }
        return null;
    }

    @Override
    public Set<UserInfo> getUserInfoSetByUserId(String userId) {
        List<OrgInfo> orgInfoList = userOrgService.getOrgInfoByUserId(userId);
        if (CollectionUtils.isEmpty(orgInfoList)){
            return null;
        }
        List<String> orgCodeList = orgInfoList.stream().map(OrgInfo::getOrgCode).collect(Collectors.toList());
        Set<UserInfo> userInfoSet = new HashSet<>();
        List<UserInfo> userInfoList = userOrgService.getUserListByOrg(orgCodeList);
        if (CollectionUtils.isNotEmpty(userInfoList)){
            for (UserInfo user : userInfoList) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserName(user.getUserName());
                userInfo.setUserCode(user.getUserCode());
                userInfoSet.add(userInfo);
            }
        }
        return userInfoSet;
    }
}
